package com.xjl.app.service;

import com.xjl.app.EntityDTO.DeviceDTO;
import com.xjl.app.convert.DeviceDTODOConvert;
import com.xjl.app.domain.EntityDO.DeviceDO;
import com.xjl.app.domain.convert.DeviceDOConvert;
import com.xjl.app.domain.service.DeviceDOService;
import com.xjl.app.infra.entity.Devices;
import com.xjl.app.infra.service.DevicesService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class DeviceDTOService {

    private final DevicesService devicesService;
    private final DeviceDOService deviceDOService;
    private final DeviceDTODOConvert instance = DeviceDTODOConvert.INSTANCE;


    public Devices save(DeviceDTO deviceDTO) {
        DeviceDO deviceDO = instance.toDeviceDO(deviceDTO);
        Devices devices = DeviceDOConvert.INSTANCE.deviceDTOtoDeviceDO(deviceDO);
        if(devicesService.save(devices)){
            return devices;
        }else {
            return null;
        }
    }

    public DeviceDTO getDeviceById(Integer deviceId) {
        Devices byId = devicesService.getById(deviceId);
        if(byId==null){
            log.error("根据Id 查找Devices为空! id= {}",deviceId);
            return null;
        }
        DeviceDTO deviceDTO = instance.toDeviceDTO(byId);
        return deviceDTO;
    }
}
